package com.ht.module.file.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.abnormal.HtException;
import com.ht.config.dataAuth.DataScope;
import com.ht.constant.BusConstant;
import com.ht.module.file.entity.FileFolder;
import com.ht.module.file.entity.FileInfo;
import com.ht.module.file.mapper.FileFolderMapper;
import com.ht.module.file.service.FileFolderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.module.file.service.FileInfoService;
import com.ht.module.file.vo.FileAndFolderVo;
import com.ht.module.sys.entity.SysRoleAuth;
import com.ht.module.sys.mapper.SysRoleAuthMapper;
import com.ht.util.CommMethod;
import com.ht.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文件夹表 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2024-06-12
 */
@Service
public class FileFolderServiceImpl extends ServiceImpl<FileFolderMapper, FileFolder> implements FileFolderService {
    @Resource
    private FileInfoService fileInfoService;
    @Resource
    private SysRoleAuthMapper sysRoleAuthMapper;

    @Override
    public List<FileAndFolderVo> findLoginUserFolderByPId(FileFolder en, String pid) {
        String userId = UserUtil.loginVerify();
        //判断是否有查询条件
        QueryWrapper<FileFolder> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(en.getFolderName())) {
            qw.eq("folder_name", en.getFolderName());
        }
        qw.eq("parent_id", pid);
        qw.eq("create_user", userId);
        qw.orderByDesc("create_date");
        //获取文件夹集合
        List<FileFolder> fileFolders = baseMapper.selectList(qw);

        //获取文件集合=
        QueryWrapper<FileInfo> qwFile = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(en.getFolderName())) {
            qwFile.eq("file_name", en.getFolderName());
        }
        qwFile.eq("folder_id", pid);
        qwFile.eq("create_user", userId);
        qwFile.orderByDesc("create_date");
        List<FileInfo> fileInfos = fileInfoService.list(qwFile);

        //构建返回集合
        List<FileAndFolderVo> vos = new ArrayList<>();
        for (FileFolder info : fileFolders) {
            FileAndFolderVo vo = new FileAndFolderVo()
                    .setId(info.getFolderId())
                    .setName(info.getFolderName())
                    .setFileFolder(info)
                    .setCreateUser(info.getCreateUser())
                    .setCreateDate(info.getCreateDate())
                    .setDeptId(info.getDeptId())
                    .setType("folder");
            vos.add(vo);
        }

        for (FileInfo info : fileInfos) {
            FileAndFolderVo vo = new FileAndFolderVo()
                    .setId(info.getFileId())
                    .setName(info.getFileName())
                    .setFileInfo(info)
                    .setCreateUser(info.getCreateUser())
                    .setCreateDate(info.getCreateDate())
                    .setDeptId(info.getDeptId())
                    .setType("file");
            vos.add(vo);
        }
        return vos;
    }


    @Override
    public void delFolderByFolderId(String folderId) {
        //查询是有权限操作当前办件
        List<Map<String, Object>> auths = sysRoleAuthMapper.findTableListByAuth(
                new QueryWrapper<SysRoleAuth>()
                        .eq("folder_id", folderId)
                , "file_folder", new DataScope()
        );

        //删除
        if(!auths.isEmpty()){
            baseMapper.deleteById(folderId);
        }

    }

    @Override
    public List<Tree<String>> findFolderTreeByLogin() {
        //获取当前登录用户的菜单
        List<FileFolder> folders = baseMapper.selectList(
                new QueryWrapper<FileFolder>()
                        .eq("create_user", UserUtil.loginVerify())
        );

        //构建树配置
        TreeNodeConfig config=new TreeNodeConfig();
        config.setIdKey("folderId");
        config.setParentIdKey("parentId");
        //定义转换器
        List<Tree<String>> build = TreeUtil.build(folders, "0", config, (node, tree) -> {
            tree.setId(node.getFolderId());
            tree.setParentId(node.getParentId());
            tree.setName(node.getFolderName());
            CommMethod.initExtra(node,tree);
        });
        return build;
    }
}
